import { TForm, TModalResult } from "../hcl/Forms.js";
import { TLable, TCombobox, TEdit, TListBox, TButton, TCheckBox, TNumberEdit, TValueMask } from "../hcl/StdCtrls.js";
import { TParaLineSpaceMode } from "./HCParaStyle.js";
import { system } from "../hcl/System.js";

export class TFrmParagraph extends TForm {
    constructor() {
        super(310, 240);
        this.caption = "段落属性设置";

        let lbl1 = new TLable("行间距");
        lbl1.left = 28;
        lbl1.top = 16;
        lbl1.width = 40;

        this.cbbSpaceMode = new TCombobox("");
        this.cbbSpaceMode.static = true;
        this.cbbSpaceMode.left = 70;
        this.cbbSpaceMode.top = 13;
        this.cbbSpaceMode.width = 77;
        this.cbbSpaceMode.addItem("单倍");
        this.cbbSpaceMode.addItem("1.15倍");
        this.cbbSpaceMode.addItem("1.5倍");
        this.cbbSpaceMode.addItem("2倍");
        this.cbbSpaceMode.addItem("最小值");
        this.cbbSpaceMode.addItem("固定值");
        this.cbbSpaceMode.addItem("多倍");
        this.cbbSpaceMode.onSelectedIndexChange = () => { this._spaceModeChange(); }

        this.edtLineSpace = new TEdit("10");
        this.edtLineSpace.left = 168;
        this.edtLineSpace.top = 13;
        this.edtLineSpace.width = 88;

        this.lblUnit = new TLable("磅");
        this.lblUnit.left = 262;
        this.lblUnit.top = 16;
        this.lblUnit.width = 12;

        let lbl3 = new TLable("水平对齐");
        lbl3.left = 16;
        lbl3.top = 47;
        lbl3.width = 52;

        this.cbbAlignHorz = new TCombobox("");
        this.cbbAlignHorz.static = true;
        this.cbbAlignHorz.left = 70;
        this.cbbAlignHorz.top = 44;
        this.cbbAlignHorz.width = 77;
        this.cbbAlignHorz.addItem("左");
        this.cbbAlignHorz.addItem("居中");
        this.cbbAlignHorz.addItem("右");
        this.cbbAlignHorz.addItem("两端");
        this.cbbAlignHorz.addItem("分散");

        let lbl4 = new TLable("垂直对齐");
        lbl4.left = 165;
        lbl4.top = 47;
        lbl4.width = 52;

        this.cbbAlignVert = new TCombobox("");
        this.cbbAlignVert.static = true;
        this.cbbAlignVert.left = 219;
        this.cbbAlignVert.top = 44;
        this.cbbAlignVert.width = 67;
        this.cbbAlignVert.addItem("上");
        this.cbbAlignVert.addItem("居中");
        this.cbbAlignVert.addItem("下");

        let lbl5 = new TLable("首行缩进");
        lbl5.left = 16;
        lbl5.top = 81;
        lbl5.width = 52;

        this.edtFirstIndent = new TNumberEdit("8");
        this.edtFirstIndent.valueMask = TValueMask.Number;
        this.edtFirstIndent.left = 70;
        this.edtFirstIndent.top = 78;
        this.edtFirstIndent.width = 40;

        let lbl6 = new TLable("毫米");
        lbl6.left = 116;
        lbl6.top = 81;
        lbl6.width = 24;

        let lbl7 = new TLable("左缩进");
        lbl7.left = 28;
        lbl7.top = 116;
        lbl7.width = 40;

        this.edtLeftIndent = new TNumberEdit("10");
        this.edtLeftIndent.valueMask = TValueMask.Number;
        this.edtLeftIndent.left = 70;
        this.edtLeftIndent.top = 113;
        this.edtLeftIndent.width = 40;

        let lbl8 = new TLable("毫米");
        lbl8.left = 116;
        lbl8.top = 116;
        lbl8.width = 24;

        let lbl9 = new TLable("右缩进");
        lbl9.left = 165;
        lbl9.top = 116;
        lbl9.width = 40;

        this.edtRightIndent = new TNumberEdit("10");
        this.edtRightIndent.valueMask = TValueMask.Number;
        this.edtRightIndent.left = 207;
        this.edtRightIndent.top = 113;
        this.edtRightIndent.width = 40;

        let lbl10 = new TLable("毫米");
        lbl10.left = 253;
        lbl10.top = 116;
        lbl10.width = 24;

        this.chkBreakRough = new TCheckBox("直观换行（换行时不判断相同类型字符同时换行）");
        this.chkBreakRough.left = 16;
        this.chkBreakRough.top = 152;
        
        this.btnOk = new TButton("确定");
        this.btnOk.onClick = () => {
            this.modalResult = TModalResult.Ok;
            this.close();
        }
        this.btnOk.left = Math.trunc((this.width - this.btnOk.width) / 2);
        this.btnOk.top = this.clientArea.height -30;

        this.beginUpdate();
        try {
            this.addControl(lbl1);
            this.addControl(this.cbbSpaceMode);
            this.addControl(this.edtLineSpace);
            this.addControl(this.lblUnit);
            this.addControl(lbl3);
            this.addControl(this.cbbAlignHorz);
            this.addControl(lbl4);
            this.addControl(this.cbbAlignVert);
            this.addControl(lbl5);
            this.addControl(this.edtFirstIndent);
            this.addControl(lbl6);
            this.addControl(lbl7);
            this.addControl(this.edtLeftIndent);
            this.addControl(lbl8);
            this.addControl(lbl9);
            this.addControl(this.edtRightIndent);
            this.addControl(lbl10);
            this.addControl(this.chkBreakRough);
            this.addControl(this.btnOk);
        } finally {
            this.endUpdate();
        }
    }

    _spaceModeChange() {
        if (this.cbbSpaceMode.itemIndex == 5) {  // 固定值
            this.edtLineSpace.text = "12";
            this.edtLineSpace.visible = true;
            this.lblUnit.text = "磅";
            this.lblUnit.visible = true;
        } else if (this.cbbSpaceMode.itemIndex == 6) {  // 多倍
            this.edtLineSpace.text = "3";
            this.edtLineSpace.visible = true;
            this.lblUnit.text = "倍";
            this.lblUnit.visible = true;
        } else {
            this.edtLineSpace.text = "";
            this.edtLineSpace.visible = false;
            this.lblUnit.visible = false;
        }
    }

    SetView(hcView) {
        this.beginUpdate();
        try {
            this.cbbSpaceMode.itemIndex = parseInt(hcView.Style.ParaStyles[hcView.CurParaNo].LineSpaceMode);
            this._spaceModeChange();

            switch (hcView.Style.ParaStyles[hcView.CurParaNo].LineSpaceMode) {
                case TParaLineSpaceMode.PLSFix:
                    this.edtLineSpace.text = String.formatFloat(1, hcView.Style.ParaStyles[hcView.CurParaNo].LineSpace);
                    break;

                case TParaLineSpaceMode.PLSMult:
                    this.edtLineSpace.text = String.formatFloat(1, hcView.Style.ParaStyles[hcView.CurParaNo].LineSpace);
                    break;
            }
            this.cbbAlignHorz.itemIndex = parseInt(hcView.Style.ParaStyles[hcView.CurParaNo].AlignHorz);
            this.cbbAlignVert.itemIndex = parseInt(hcView.Style.ParaStyles[hcView.CurParaNo].AlignVert);
            //clrbxBG.Color := AHCView.Style.ParaStyles[AHCView.CurParaNo].BackColor;
            this.edtFirstIndent.text = String.formatFloat(1, hcView.Style.ParaStyles[hcView.CurParaNo].FirstIndent);
            this.edtLeftIndent.text = String.formatFloat(1, hcView.Style.ParaStyles[hcView.CurParaNo].LeftIndent);
            this.edtRightIndent.text = String.formatFloat(1, hcView.Style.ParaStyles[hcView.CurParaNo].RightIndent);
            this.chkBreakRough.checked = hcView.Style.ParaStyles[hcView.CurParaNo].BreakRough;

            this.moveCenter();
        } finally {
            this.endUpdate();
        }

        this.showModal(() => {
            hcView.BeginUpdate();
            try {
                let vFloat = 12;
                if (this.cbbSpaceMode.itemIndex > 4)
                    vFloat = system.parseFloatDef(this.edtLineSpace.text, 0);

                hcView.ApplyParaLineSpace(this.cbbSpaceMode.itemIndex, vFloat);
                hcView.ApplyParaAlignHorz(this.cbbAlignHorz.itemIndex);
                hcView.ApplyParaAlignVert(this.cbbAlignVert.itemIndex);
                //hcView.ApplyParaBackColor(clrbxBG.Color);
                hcView.ApplyParaFirstIndent(system.parseFloatDef(this.edtFirstIndent.text, 0));
                hcView.ApplyParaLeftIndent(system.parseFloatDef(this.edtLeftIndent.text, 0));
                hcView.ApplyParaRightIndent(system.parseFloatDef(this.edtRightIndent.text, 0));
                hcView.ApplyParaBreakRough(this.chkBreakRough.checked);
            } finally {
                hcView.EndUpdate();
            }
        });
    }
}